<template>
	<view id="Container">
		<view class="searchContainer">
			<search @search="gotoSearch"></search>
		</view>
		<!-- 轮播图 -->
		<swiper id="swiperContainer" :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
			<swiper-item v-for="(item,index) in swiperList" :key="item.goods_id">
				<navigator :url="'/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id">
					<image :src="item.image_src" mode="widthFix"></image>
				</navigator>
			</swiper-item>
		</swiper>
		<!-- 首页分类 -->
		<view class="cateListContainer">
			<view @click="switchToCate(item)" v-for="(item,index) in cateList" :key="index" class="cateItem">
				<image :src="item.image_src" mode=""></image>
			</view>
		</view>
		<!-- 楼层 -->
		<view class="floorContainer">
			<view v-for="(item,index) in floorList" :key="index" class="flooritem">
				<image class="titleImg" :src="item.floor_title.image_src" mode=""></image>
				<view class="itemContainer">
					<navigator :url="item.product_list[0].url" class="bigImg">
						<image :style="{width:item.product_list[0].image_width+'rpx'}" :src="item.product_list[0].image_src" mode="widthFix"></image>
					</navigator>
					<view class="smallImgContainer">
						<navigator :url="small.url" v-for="(small,index2) in item.product_list" v-if="index2!==0" :key="index2" class="smallImg">
							<image :style="{width:small.image_width+'rpx'}" :src="small.image_src" mode="widthFix"></image>
						</navigator>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				swiperList:[],
				cateList:[],
				floorList:[]
			};
		},
		onLoad() {
			this.getSwiperList()
			this.getHomeCateList()
			this.getFloorList()
		},
		methods:{
			// 获得轮播图数据
			async getSwiperList(){
				const {data:res} = await uni.$http.get('/api/public/v1/home/swiperdata')
				// 请求成功
				if(res.meta.status == 200){
					this.swiperList = res.message
				}else{
					return uni.$showMsg()
				}
			},
			// 获取首页分类选项数据
			async getHomeCateList(){
				const {data:res} = await uni.$http.get("/api/public/v1/home/catitems")
				if(res.meta.status == 200){
					this.cateList = res.message
				}else{
					return uni.$showMsg()
				}
			},
			// 点击分类跳转到分类界面
			switchToCate(item){
				if(item.name == "分类"){
					uni.switchTab({
						url:"/pages/cate/cate"
					})
				}
			},
			// 获取楼层数据
			async getFloorList(){
				const {data:res} = await uni.$http.get("/api/public/v1/home/floordata")
				if(res.meta.status == 200){
					res.message.forEach(item=>{
						item.product_list.forEach(floor =>{
							floor.url = "/subpkg/goods_list/goods_list?" + floor.navigator_url.split("?")[1]
						})
					})
					this.floorList = res.message
				}else{
					return uni.$showMsg()
				}
			},
			gotoSearch(){
				uni.navigateTo({
					url:"/subpkg/search/search"
				})
			}
		}
	}
</script>

<style lang="scss">
	.searchContainer{
		position: sticky;
		top: 0;
		z-index: 999;
	}
	#swiperContainer{
		height: 330rpx;
		swiper-item{
			width: 100%;
			navigator{
				width: 100%;
				height: 100%;
				image{
					width: 100%;
					height: 100%;
				}
			}
		}
	}
	
	.cateListContainer{
		display: flex;
		justify-content: space-around;
		margin: 15rpx 0;
		.cateItem{
			image{
				width: 128rpx;
				height: 140rpx;
			}
		}
	}
	
	.flooritem{
		.titleImg{
			height: 60rpx;
			width: 100%;
			display: flex;
		}
		.itemContainer{
			display: flex;
			padding-left: 10rpx;
			.smallImgContainer{
				display: flex;
				flex-wrap: wrap;
				justify-content: space-around;
			}
		}	
	}

</style>
